/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 9, 2013  3:46:21 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.promis.impl
 * File Name : GlPostingDetailDaoImpl.java
 * TODO
 */
package com.tis.database.dao.promis.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import promis.utility.CommonService;
import com.tis.database.dao.impl.AbstractDaoImpl;
import com.tis.database.dao.promis.GlPostingDetailDao;
import com.tis.database.entitys.promis.GlPostingDetail;
import com.tis.database.entitys.promis.GlPostingDetailPK;
import com.tis.database.entitys.promis.VwSyncGlPostingDetail;
import com.tis.database.entitys.staging.FiCreditNoteOutb;
import com.tis.database.entitys.staging.FiDepositReturnOutb;
import com.tis.database.entitys.staging.FiInvReceiptOutb;
import com.tis.database.entitys.staging.FiInvoiceOutb;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 9, 2013 : 3:46:21 PM 
 * CreateBy=Chatree Siangklom
 * 
 * GlPostingDetailDaoImpl
 * TODO
 */
@Repository
@SuppressWarnings("unchecked")
public class GlPostingDetailDaoImpl extends AbstractDaoImpl<GlPostingDetail, GlPostingDetailPK> implements GlPostingDetailDao {
	private static final Logger logging = LoggerFactory.getLogger(GlPostingDetailDaoImpl.class);
	
	/**
	 * Date =Oct 9, 2013 : 3:46:52 PM 
	 * CreateBy=Chatree Siangklom
	 * GlPostingDetailDaoImpl
	 * TODO
	 * @param entityClass
	 */
	protected GlPostingDetailDaoImpl() {
		super(GlPostingDetail.class);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.lang.Object)
	 * Date =Oct 9, 2013 : 3:54:12 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(GlPostingDetail e) throws HibernateException {
		super.saveOrUpdate(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.util.List)
	 * Date =Oct 9, 2013 : 3:54:51 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(List<GlPostingDetail> e) throws HibernateException {
		super.saveOrUpdate(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#findById(java.io.Serializable)
	 * Date =Oct 9, 2013 : 3:56:19 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public GlPostingDetail findById(GlPostingDetailPK id) throws HibernateException {
		return super.findById(id);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#findByCriteria(org.hibernate.criterion.Criterion)
	 * Date =Oct 9, 2013 : 3:56:38 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<GlPostingDetail> findByCriteria(Criterion criterion) throws HibernateException {
		return super.findByCriteria(criterion);
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.promis.GlPostingDetailDao#Search(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, int)
	 * Date =Oct 9, 2013 : 3:46:49 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<GlPostingDetail> Search(
										String company, 
										String project,
										String voucherType, 
										String voucherSeries, 
										String voucherNumber,
										String glPostingSeqNumber, 
										String syncIsstaging,
										String syncIssync,
										String unitNumber,
										String status,
										String voucherDateStart,
										String voucherDateEnd, 
										int maxResult) throws HibernateException, Exception {

		try {
			
			Session session = getCurrentSession();
			Query q = session.getNamedQuery(VwSyncGlPostingDetail.findBySearch);
			String query = q.getQueryString();
			
			if(!CommonService.IsNull(company))
				query += " And upper(c.id.company) like '%"+ CommonService.IsNullRString(company).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(project))
				query += " And upper(c.id.project) like '%"+ CommonService.IsNullRString(project).toUpperCase() +"%' ";
			
			if(!CommonService.IsNull(voucherType))
				query += " And upper(c.id.voucherType) like '%"+ CommonService.IsNullRString(voucherType).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(voucherSeries))
				query += " And upper(c.id.voucherSeries) like '%"+ CommonService.IsNullRString(voucherSeries).toUpperCase() +"%'";
						
			if(!CommonService.IsNull(voucherNumber))
				query += " And upper(c.id.voucherNumber) like '%"+ CommonService.IsNullRString(voucherNumber).toUpperCase() +"%'";					
			
			if(!CommonService.IsNull(glPostingSeqNumber))
				query += " And upper(c.id.glPostingSeqNumber) like '%"+ CommonService.IsNullRString(glPostingSeqNumber).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(status))
				query += " And upper(c.status) like '%"+ CommonService.IsNullRString(status).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(syncIsstaging))
				query += " And upper(c.syncIsstaging) like '%"+ CommonService.IsNullRString(syncIsstaging).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(syncIssync))
				query += " And upper(c.syncIssync) like '%"+ CommonService.IsNullRString(syncIssync).toUpperCase() +"%'";	
			
			if(!CommonService.IsNull(unitNumber))
				query += " And upper(c.unitNumber) like '%"+ CommonService.IsNullRString(unitNumber).toUpperCase() +"%'";				
			
			if(!CommonService.IsNull(voucherDateStart) && !CommonService.IsNull(voucherDateEnd))
				query += " And to_char(c.voucherDate, 'YYYY-MM-DD') BETWEEN :voucherDateStart And :voucherDateEnd ";
			
			query += " Order by :p_orderby "; 	
			
			logging.info(query);
			
			q = session.createQuery(query);
			
			if(!CommonService.IsNull(voucherDateStart))
				q.setParameter("voucherDateStart", CommonService.IsNullRString(voucherDateStart).toUpperCase());
			
			if(!CommonService.IsNull(voucherDateEnd))
				q.setParameter("voucherDateEnd", CommonService.IsNullRString(voucherDateEnd).toUpperCase());
			
			q.setParameter("p_orderby", " c.id.glPostingSeqNumber asc ");
			
			if(maxResult > 0)
				q.setMaxResults(maxResult);			
			
			List<GlPostingDetail> lst = Collections.checkedList(q.list(), GlPostingDetail.class);
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(HibernateException ex) {
			logging.error(ex.getMessage());
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex1) {
			logging.error(ex1.getMessage());
			ex1.printStackTrace();
			throw ex1;
		}
	}

	@Override
	public List<GlPostingDetail> UpdateBackSapMessageRecv(List<FiInvReceiptOutb> lst) throws HibernateException, Exception {
		List<GlPostingDetail> newlst = new ArrayList<GlPostingDetail>();
		try{
			
			if(CommonService.getSize(lst) > 0){
				
				for(FiInvReceiptOutb value : lst){
					
					GlPostingDetailPK id = new GlPostingDetailPK();
					id.setCompany(value.getCompany());
					id.setGlPostingSeqNumber(value.getGlPostingSeqNumber().longValue());
					id.setVoucherNumber(CommonService.ConvertStringToLong(value.getVoucherNumber()));
					id.setVoucherSeries(value.getVoucherSeries());
					id.setVoucherType(value.getVoucherType());
					
					GlPostingDetail obj = findById(id);
					
					if(obj != null){
						
						obj.setSyncInstanceNo(value.getInstanceNo());
						obj.setSyncExternalRefKey(value.getExtRefKey());
						obj.setSyncExternalRefNo(value.getExtRefNo());
						obj.setSyncIsstaging("Y");
						obj.setSyncIssync(value.getSyncIssync());
						obj.setSyncUpdatedby(value.getSyncUpdatedby());
						obj.setSyncUpdatedon(value.getSyncUpdatedon());						
						
						newlst.add(obj);
					}else 
						throw new Exception("find not fund Update back to GlPostingDetail GlPostingDetailPK value=" + id.toString());
				}
				
				saveOrUpdate(newlst);
			}
			
		}catch(Exception e){
			newlst = null;
			e.printStackTrace();
			logging.error(e.getMessage());
			throw e;
		}
		
		return newlst;
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.promis.GlPostingDetailDao#UpdateBackSapMessageInv(java.util.List)
	 * Date =Oct 15, 2013 : 3:02:00 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<GlPostingDetail> UpdateBackSapMessageInv(List<FiInvoiceOutb> lst) throws HibernateException, Exception {
		// TODO Auto-generated method stub
		List<GlPostingDetail> newlst = new ArrayList<GlPostingDetail>();
		try{
			
			if(CommonService.getSize(lst) > 0){
				
				for(FiInvoiceOutb value : lst){
					
					GlPostingDetailPK id = new GlPostingDetailPK();
					id.setCompany(value.getCompany());
					id.setGlPostingSeqNumber(value.getGlPostingSeqNumber().longValue());
					id.setVoucherNumber(CommonService.ConvertStringToLong(value.getVoucherNumber()));
					id.setVoucherSeries(value.getVoucherSeries());
					id.setVoucherType(value.getVoucherType());
					
					GlPostingDetail obj = findById(id);
					
					if(obj != null){
						
						obj.setSyncInstanceNo(value.getInstanceNo());
						obj.setSyncExternalRefKey(value.getExtRefKey());
						obj.setSyncExternalRefNo(value.getExtRefNo());
						obj.setSyncIsstaging("Y");
						obj.setSyncIssync(value.getSyncIssync());
						obj.setSyncUpdatedby(value.getSyncUpdatedby());
						obj.setSyncUpdatedon(value.getSyncUpdatedon());						
						
						newlst.add(obj);
					}else 
						throw new Exception("find not fund Update back to GlPostingDetail GlPostingDetailPK value=" + id.toString());
				}
				
				saveOrUpdate(newlst);
			}
			
		}catch(Exception e){
			newlst = null;
			e.printStackTrace();
			logging.error(e.getMessage());
			throw e;
		}
		
		return newlst;
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.promis.GlPostingDetailDao#UpdateBackSapMessageCN(java.util.List)
	 * Date =Oct 15, 2013 : 3:02:00 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<GlPostingDetail> UpdateBackSapMessageCN(List<FiCreditNoteOutb> lst) throws HibernateException, Exception {
		List<GlPostingDetail> newlst = new ArrayList<GlPostingDetail>();
		try{
			
			if(CommonService.getSize(lst) > 0){
				
				for(FiCreditNoteOutb value : lst){
					
					GlPostingDetailPK id = new GlPostingDetailPK();
					id.setCompany(value.getCompany());
					id.setGlPostingSeqNumber(value.getGlPostingSeqNumber().longValue());
					id.setVoucherNumber(CommonService.ConvertStringToLong(value.getVoucherNumber()));
					id.setVoucherSeries(value.getVoucherSeries());
					id.setVoucherType(value.getVoucherType());
					
					GlPostingDetail obj = findById(id);
					
					if(obj != null){
						
						obj.setSyncInstanceNo(value.getInstanceNo());
						obj.setSyncExternalRefKey(value.getExtRefKey());
						obj.setSyncExternalRefNo(value.getExtRefNo());
						obj.setSyncIsstaging("Y");
						obj.setSyncIssync(value.getSyncIssync());
						obj.setSyncUpdatedby(value.getSyncUpdatedby());
						obj.setSyncUpdatedon(value.getSyncUpdatedon());						
						
						newlst.add(obj);
					}else 
						throw new Exception("find not fund Update back to GlPostingDetail GlPostingDetailPK value=" + id.toString());
				}
				
				saveOrUpdate(newlst);
			}
			
		}catch(Exception e){
			newlst = null;
			e.printStackTrace();
			logging.error(e.getMessage());
			throw e;
		}
		
		return newlst;
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.promis.GlPostingDetailDao#UpdateBackSapMessageDepo(java.util.List)
	 * Date =Oct 15, 2013 : 3:02:00 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<GlPostingDetail> UpdateBackSapMessageDepo(List<FiDepositReturnOutb> lst) throws HibernateException, Exception {
		List<GlPostingDetail> newlst = new ArrayList<GlPostingDetail>();
		try{
			
			if(CommonService.getSize(lst) > 0){
				
				for(FiDepositReturnOutb value : lst){
					
					GlPostingDetailPK id = new GlPostingDetailPK();
					id.setCompany(value.getCompany());
					id.setGlPostingSeqNumber(value.getGlPostingSeqNumber().longValue());
					id.setVoucherNumber(CommonService.ConvertStringToLong(value.getVoucherNumber()));
					id.setVoucherSeries(value.getVoucherSeries());
					id.setVoucherType(value.getVoucherType());
					
					GlPostingDetail obj = findById(id);
					
					if(obj != null){
						
						obj.setSyncInstanceNo(value.getInstanceNo());
						obj.setSyncExternalRefKey(value.getExtRefKey());
						obj.setSyncExternalRefNo(value.getExtRefNo());
						obj.setSyncIsstaging("Y");
						obj.setSyncIssync(value.getSyncIssync());
						obj.setSyncUpdatedby(value.getSyncUpdatedby());
						obj.setSyncUpdatedon(value.getSyncUpdatedon());						
						
						newlst.add(obj);
					}else 
						throw new Exception("find not fund Update back to GlPostingDetail GlPostingDetailPK value=" + id.toString());
				}
				
				saveOrUpdate(newlst);
			}
			
		}catch(Exception e){
			newlst = null;
			e.printStackTrace();
			logging.error(e.getMessage());
			throw e;
		}
		
		return newlst;
	}
	

}
